﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.PersonService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;

namespace RN.ERP.Web.Exam
{
    public partial class BmQCInfo : BasePage
    {
        PersonServiceClient client = new PersonServiceClient();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////type:0添加1查看2修改
                hidType.Value = Request.QueryString["type"];
                hidQCId.Value = Request.QueryString["qcid"];
                if (hidType.Value == "1")
                {
                    lbtnSave.Visible = false;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "SetAllFormDisabled();", true);
                }
                BindData();
            }
        }

        /// <summary>
        /// 绑定期次信息
        /// </summary>
        private void BindData()
        {
            if (!string.IsNullOrEmpty(hidQCId.Value))
            {
                int qcId = 0;
                int.TryParse(hidQCId.Value, out qcId);
                Exam_QC examQC = JsonConvert.DeserializeObject<Exam_QC>(client.GetExamQC(qcId));
                if (examQC != null)
                {
                    txtTitle.Text = examQC.Title;
                    txtCode.Text = examQC.Code;
                    txtExamDate.Text = ((DateTime)examQC.ExamDate).ToShortDateString();
                    txtStartDate.Text = ((DateTime)examQC.StartDate).ToShortDateString();
                    txtEndDate.Text = ((DateTime)examQC.EndDate).ToShortDateString();
                    txtPrintDate.Text = ((DateTime)examQC.PrintDate).ToShortDateString();
                    txtSelScoreDate.Text = ((DateTime)examQC.SelScoreDate).ToShortDateString();
                    SetExamineeType(examQC.ExamineeType);
                }
            }
        }

        /// <summary>
        /// 获取纳入考试人员类型
        /// </summary>
        /// <param name="eType">类型拼接字符串 如",1,2,"</param>
        private void SetExamineeType(string eType)
        {
            if (String.IsNullOrEmpty(eType))
            {
                return;
            }

            foreach (ListItem item in examExpertType.Items)
            {
                item.Selected = eType.Contains(String.Format(",{0},", item.Value));
            }
        }

        /// <summary>
        /// 获取纳入考试人员类型
        /// </summary>
        /// <returns>1:初始申请人员、2:资格复审人员 可全选</returns>
        private string GetExamineeType()
        {
            string typeString = ",";
            foreach (ListItem item in examExpertType.Items)
            {
                if (item.Selected)
                {
                    typeString += item.Value + ",";
                }
            }
            if (typeString.Length==1)
            {
                typeString = String.Empty;
            }
            return typeString;
        }

        /// <summary>
        /// 点击【保存】
        /// </summary>
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            if (DateTime.Parse(txtStartDate.Text) > DateTime.Parse(txtEndDate.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('报名开始时间不能大于结束时间');", true);
                return;
            }

            string examineeTypeStr = GetExamineeType();

            if (String.IsNullOrEmpty(examineeTypeStr))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('必须选择【纳入考试人员】');", true);
                return;
            }

            Exam_QC examQC = new Exam_QC();
            OperationResult result = new OperationResult
            {
                Result = false,
                Message = "操作失败"
            };

            if (!string.IsNullOrEmpty(hidQCId.Value))
            {
                int qcId = 0;
                int.TryParse(hidQCId.Value, out qcId);
                examQC.ID = qcId;
            }
            examQC.Title = txtTitle.Text;
            examQC.Code = txtCode.Text;
            examQC.ExamDate = DateTime.Parse(txtExamDate.Text);
            examQC.StartDate = DateTime.Parse(txtStartDate.Text);
            examQC.EndDate = DateTime.Parse(txtEndDate.Text);
            examQC.PrintDate = DateTime.Parse(txtPrintDate.Text);
            examQC.SelScoreDate = DateTime.Parse(txtSelScoreDate.Text);
            examQC.AppId = SystemID;
            examQC.IsrtDate = DateTime.Now;
            examQC.IsDel = false;
            examQC.ExamineeType = examineeTypeStr;

            var qc = JsonConvert.SerializeObject(examQC);
            if (hidType.Value == "0")
            {
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.AddExamQC(qc));
            }
            else if (hidType.Value == "2")
            {
                result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.EditExamQC(qc));
            }
            if (result.Result)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "','',CloseWin);", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "$.ligerDialog.warn('" + result.Message + "');", true);
            }
        }
    }
}